<?php

/**
 * Description of leilao_model
 *
 * @author cjc
 */
class Leilao_Model extends I_Model {

    protected $_table_name = 'leiloes';
    protected $_primary_key = 'ID';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'DATE';
    protected $_direction = 'DESC';
    public $rules = array();
    protected $_timestamps = TRUE;
    protected $_table_seller = 'comitentes';
    protected $_join_key_seller = 'COMITENTE_ID';
    protected $_join_primary_key = 'ID';

    public function __construct() {
        parent::__construct();
    }

    function get($id = null) {



        return $this->fullInfo($id);
    }

    function info_statistics($id) {
        return array(
            'NUM_LOTES' => $this->db->get_where('lotes', array(
                'LEILAO_ID' => $id
            ))->num_rows(),
            'HABILITADOS' => $this->db->get_where('habilitacoes', array(
                'LEILAO_ID' => $id
            ))->num_rows()
        );
    }

    function next_leiloes() {
        $this->db->where(array('l.DATA > ' => date('Y-m-d H:i:s'), 'l.STATUS <= ' => 2));
        return $this->fullInfo();
    }

    function fullInfo($id = null) {
        $dataLeilao = false;
        $this->db->select(
                        'l.*,sl.ID as COD_COMITENTE,
				    sl.RAZAO,
				    sl.NOME_FANTASIA,
				    sl.LOGO
				    ')
                ->from('leiloes as l')
                ->join('comitentes as sl', "l.COMITENTE_ID=sl.ID", 'LEFT')
                //	->join('habilitacoes as hb','hb.LEILAO_ID=l.ID','LEFT')
//		->join('lotes as lt','lt.LEILAO_ID=l.ID','LEFT')
                ->where('l.PRESTACAO_CONTAS', 'N');
        if (!is_null($id)) {
            $this->db->where(array(
                "l.{$this->_primary_key}" => $id
            ));
        }

        $this->db->order_by($this->_primary_key, $this->_direction);

        $dbres = $this->db->get();
        if ($dbres->num_rows() > 0) {
            $dataLeilao = $dbres->result();
            if (!is_null($id)) {
                $dataLeilao = $dbres->row();
                $dataLeilao->INFO = $this->info_statistics($id);
            }
        }

        return $dataLeilao;
    }

    /*
     * get_all_close Recupera informaçãoes estatisitcas de leiloes que foram finalizados e sem prestação de contas @return object data
     */

    function get_all_close($prestaca_contas = 'N') {
        $SQL = <<<EOD
   SELECT COUNT(lt.ID) as NUM_LOTES,  ha.HABILITADOS , lca.VENDIDOS,cm.RAZAO as COMITENTE, le.* FROM leiloes as le 
		JOIN lotes as lt ON le.ID = lt.LEILAO_ID
   		JOIN comitentes as cm ON le.COMITENTE_ID = cm.ID	 
		/*SELECT JOIN DATA NUM USERS HABILITADOS */
		LEFT JOIN (
		     SELECT LEILAO_ID, COUNT('ID') as HABILITADOS
			       FROM habilitacoes  GROUP BY LEILAO_ID 
		 ) as ha ON le.ID = ha.LEILAO_ID
		/*SELECT JOIN DATA LOTES ARREMTADOS */
		LEFT JOIN (
		     SELECT LEILAO_ID, COUNT('ID') as VENDIDOS
			       FROM lances_arrematados  WHERE USER_ID <> 1 GROUP BY LEILAO_ID 
		 ) AS lca ON le.ID = lca.LEILAO_ID 
		 WHERE (le.PRESTACAO_CONTAS = '$prestaca_contas' AND le.STATUS = 4) GROUP BY le.ID;

EOD;

        $q = $this->db->query($SQL);

        return $q->result();
    }

    /*
     * get_info_close Retorna informações do leilão que foi fechado com detalhes no itens lotes, comitentes, lances e lances ofertados dentro do sistema @param (int) $id codigo do leilão
     */

    function get_info_close($id) {
        $this->db->select('lca.USER_ID,lca.LANCE_ID,lca.CONDICIONAL,lca.VENDIDO,lca.FECHADO,
   		   		  lca.TOTAL as LANCE_GANHADOR,lca.DATAHORA as HORA_LANCE,us.LOGIN,cm.RAZAO,
   		   		  lca.PAGAMENTO_ID,
				  pg.CONFIRMADO,
                                  pg.PAGAMENTO_ENVIADO,
                                  pg.COMPROVANTE,
                                  pg.NFE_LIBERADO,
                                  pg.PAGAMENTO_ID as NFE_ID,
				  pg2.PAGAMENTOS_CONFIRMADOS,  
				  lt.DESCRICAO as LOTE_DESCRICAO,
				  
   		   		  lt.FECHAMENTO,
   		   		  lt.COMISSAO,
   		   		  COUNT(lt.ID) as TOTAL_LOTES,
   		   		  lt.RETIRADO,
   		   		  lt.NUM_LOTE,lt.VALOR_MINIMO,lt.LANCE_INICIAL,
   		   		  cid.NOME as CIDADE,
   		   		  lc.BIDS,
   		   		  lc.USERS,
				  
				  lca2.PENDENTES,
				  lt2.LOTES_SEM_VENDA,
				  lca3.LOTES_VENDIDOS,
   		   		  ufe.UF as UF_LOTE,
   		   		  vst.COUNT as VISITAS,
   		   		  cat.NAME as CATEGORIA,
   		   		  sub.NAME as TIPO_ATIVO,
                                  prp.APROUVE,
   		   		  le.*')->from('leiloes as le')->join('lotes as lt', 'lt.LEILAO_ID=le.ID')->join('visitas_lotes as vst', 'lt.ID=vst.LOTE_ID', 'LEFT')
                ->join('comitentes as cm', 'le.COMITENTE_ID=cm.ID')
                ->join('lances_arrematados as lca', 'lca.LOTE_ID=lt.ID', 'LEFT')
                ->join('lances_propostas as prp', 'lca.LOTE_ID=prp.LOTE_ID', 'LEFT')
                ->join('lances_pagamentos as pg', 'lca.PAGAMENTO_ID=pg.PAGAMENTO_ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,COUNT(ID) AS PENDENTES FROM lances_arrematados WHERE  CONDICIONAL = "S" AND FECHADO = "N"  GROUP BY LEILAO_ID) as lca2', 'lca2.LEILAO_ID=le.ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,COUNT(ID) AS LOTES_SEM_VENDA FROM lotes WHERE  ID NOT IN( SELECT LOTE_ID FROM lances_arrematados)  GROUP BY LEILAO_ID) as lt2', 'lt2.LEILAO_ID=le.ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,COUNT(ID) AS LOTES_VENDIDOS FROM lances_arrematados WHERE VENDIDO = "S" GROUP BY LEILAO_ID) as lca3', 'lca3.LEILAO_ID=le.ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,COUNT(PAGAMENTO_ID) AS PAGAMENTOS_CONFIRMADOS FROM lances_pagamentos WHERE LOTE_ID IN (SELECT LOTE_ID FROM lances_arrematados WHERE VENDIDO ="S") AND CONFIRMADO = "S"  GROUP BY LEILAO_ID) as pg2', 'pg2.LEILAO_ID=le.ID', 'LEFT')
                ->join('users as us', 'us.ID=lca.USER_ID', 'LEFT')
                ->join('cidades as cid', 'lt.CIDADE=cid.ID')
                ->join('tipos_ativos as sub', 'sub.ID=lt.TIPO_ID', 'LEFT')
                ->join('(SELECT LOTE_ID,COUNT(\'ID\') AS BIDS,COUNT(DISTINCT USER_ID) AS USERS FROM lances
                            GROUP BY LOTE_ID) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')->
                join('categorias as cat', 'cat.ID=sub.CATEGORIA_ID', 'LEFT')->join('estados as ufe', 'cid.ESTADO=ufe.ID')->where('le.ID', $id)->group_by('lt.ID')->order_by('lt.NUM_LOTE', 'ASC');

        $q = $this->db->get();

        return $q->result();
    }

    /*
     * get_info_close Retorna informações do leilão que foi fechado com detalhes no itens lotes, comitentes, lances e lances ofertados dentro do sistema @param (int) $id codigo do leilão
     */

    function get_statistics_close($id) {

        $this->db->trans_start();
        $this->db->select('le.ID,cm.RAZAO as COMITENTE,COUNT(DISTINCT hb.USER_ID) as HABILITADOS, 
				COUNT(DISTINCT lt.ID) AS TOTAL_LOTES, COUNT(DISTINCT lc.ID) AS BIDS , COUNT(DISTINCT lc.USER_ID) AS USERS_BIDS,pf.ESTADOS  ')
                ->from('leiloes as le')
                ->join('comitentes as cm', 'cm.ID=le.COMITENTE_ID')
                ->join('habilitacoes as hb', 'hb.LEILAO_ID=le.ID')
                ->join('lotes as lt', 'lt.LEILAO_ID=le.ID')
                ->join('lances as lc', 'lc.LEILAO_ID=le.ID')
                ->join('users as us', 'us.ID=hb.USER_ID')
                ->join('(SELECT USER_ID,COUNT(DISTINCT UF) AS ESTADOS FROM profiles GROUP BY USER_ID) AS pf', 'pf.USER_ID=us.ID')
                ->where('le.ID', $id);

        $q = $this->db->get();


        $data = new stdClass();

        $data->leilao = $q->row();
        //com lances

        $this->db->select('lc.LANCE,lc.NUM,lt.VMV,lt.LANCE_INICIAL')
                ->from('leiloes as le')
                ->join('(SELECT ID,LEILAO_ID,SUM(LANCE_INICIAL) AS LANCE_INICIAL,
				SUM(VALOR_MINIMO) AS VMV
				FROM lotes WHERE ID IN( SELECT LOTE_ID FROM lances )
				GROUP BY LEILAO_ID) as lt', 'lt.LEILAO_ID=le.ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,SUM(VALOR) AS LANCE,COUNT(DISTINCT LOTE_ID) AS NUM 
				FROM lances GROUP BY LEILAO_ID) as lc', 'lc.LEILAO_ID=le.ID', 'LEFT')
                ->where('le.ID', $id);
        $q = $this->db->get();
        $data->lotes_com_lances = $q->row();


        $this->db->select('lca.LANCE,lca.NUM,lt.LANCE_INICIAL,lt.VMV')
                ->from('leiloes as le')
                ->join('(SELECT ID,LEILAO_ID,SUM(LANCE_INICIAL) AS LANCE_INICIAL,
				SUM(VALOR_MINIMO) AS VMV
				FROM lotes WHERE ID IN( SELECT LOTE_ID FROM lances_arrematados )  
				GROUP BY LEILAO_ID) as lt', 'lt.LEILAO_ID=le.ID', 'LEFT')
                ->join('(SELECT LEILAO_ID,SUM(TOTAL) AS LANCE,COUNT(LOTE_ID) AS NUM
				FROM lances_arrematados WHERE CONDICIONAL = "N" GROUP BY LEILAO_ID) as lca', 'lca.LEILAO_ID=le.ID', 'LEFT')
                ->where('le.ID', $id);
        $q = $this->db->get();
        $data->lotes_vendidos = $q->row();

        $this->db->select('lca.LANCE,lca.NUM,lt.VMV,lt.LANCE_INICIAL')
                ->from('leiloes as le')
                ->join('(SELECT ID,LEILAO_ID,SUM(LANCE_INICIAL) AS LANCE_INICIAL,
				SUM(VALOR_MINIMO) AS VMV
				FROM lotes WHERE ID IN( SELECT LOTE_ID FROM lances_arrematados WHERE CONDICIONAL = "S")
				GROUP BY LEILAO_ID) as lt', 'lt.LEILAO_ID=le.ID', 'left')
                ->join('(SELECT LEILAO_ID,SUM(TOTAL) AS LANCE,COUNT(LOTE_ID) AS NUM
				FROM lances_arrematados WHERE CONDICIONAL = "S" GROUP BY LEILAO_ID) as lca', 'lca.LEILAO_ID=le.ID', 'LEFT')
                ->where('le.ID', $id);
        $q = $this->db->get();
        $data->lotes_condicionais = $q->row();


        $this->db->select('lt.LANCE_INICIAL,lt.NUM,lt.VMV')
                ->from('leiloes as le')
                ->join('(SELECT ID,LEILAO_ID,SUM(LANCE_INICIAL) AS LANCE_INICIAL,COUNT(ID) AS NUM,
				SUM(VALOR_MINIMO) AS VMV
				FROM lotes WHERE ID NOT IN( SELECT LOTE_ID FROM lances_arrematados )  GROUP BY LEILAO_ID) as lt', 'lt.LEILAO_ID=le.ID', 'LEFT')
                ->where('le.ID', $id);
        $q = $this->db->get();
        $data->lotes_sem_lances = $q->row();


        $this->db->select('pf.*,uuf.UF as ESTADO')
                ->from('lances  as lc')
                ->join('users as us', 'us.ID=lc.USER_ID')
                ->join('(SELECT USER_ID,UF,CIDADE FROM profiles GROUP BY USER_ID) AS pf', 'pf.USER_ID=us.ID')
                ->join('cidades as cid', 'cid.NOME=pf.CIDADE')
                ->join('estados as uuf', 'cid.ESTADO=uuf.ID')
                ->where('lc.LEILAO_ID', $id);
        $q = $this->db->get();
        $data->estados = $q->result();
        if ($this->db->trans_complete()) {

            return $data;
        }
        return $q->row();
    }

    function get_lotes_vendidos($id) {


        $this->db->select('lca.USER_ID,lca.LANCE_ID,lca.CONDICIONAL,
				  lca.VENDIDO,lca.FECHADO,
   		   		  lca.TOTAL as LANCE_GANHADOR,lca.DATAHORA as HORA_LANCE,us.LOGIN,cm.RAZAO,
   		   		  lt.DESCRICAO as LOTE_DESCRICAO,
   		   		  lt.FECHAMENTO,
   		   		  lt.COMISSAO,
   		   		  COUNT(lt.ID) as TOTAL_LOTES,
   		   		  lt.RETIRADO,
   		   		  lt.NUM_LOTE,lt.VALOR_MINIMO,lt.LANCE_INICIAL,
   		   		  cid.NOME as CIDADE,
   		   		  lc.BIDS,
   		   		  lc.USERS,
   		   		  ufe.UF as UF_LOTE,
   		   		  vst.COUNT as VISITAS,
   		   		  cat.NAME as CATEGORIA,
   		   		  sub.NAME as TIPO_ATIVO,
   		   		  le.*')->from('leiloes as le')
                ->join('(SELECT * FROM lotes WHERE ID IN ( SELECT LOTE_ID FROM lances_arrematados WHERE VENDIDO = "S") GROUP BY ID )  as lt', 'lt.LEILAO_ID=le.ID')
                ->join('visitas_lotes as vst', 'lt.ID=vst.LOTE_ID', 'LEFT')
                ->join('comitentes as cm', 'le.COMITENTE_ID=cm.ID')
                ->join('lances_arrematados as lca', 'lca.LOTE_ID=lt.ID', 'LEFT')
                ->join('users as us', 'us.ID=lca.USER_ID', 'LEFT')
                ->join('cidades as cid', 'lt.CIDADE=cid.ID')
                ->join('tipos_ativos as sub', 'sub.ID=lt.TIPO_ID', 'LEFT')
                ->join('(SELECT LOTE_ID,COUNT(\'ID\') AS BIDS,COUNT(DISTINCT USER_ID) AS USERS FROM lances
                            GROUP BY LOTE_ID) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('categorias as cat', 'cat.ID=sub.CATEGORIA_ID', 'LEFT')
                ->join('estados as ufe', 'cid.ESTADO=ufe.ID')
                ->where('le.ID', $id)
                ->group_by('lt.ID')
                ->order_by('lt.NUM_LOTE', 'ASC');

        $q = $this->db->get();

        return $q->result();
        echo '<pre>';
        print_r($q->result());
    }

    function get_lotes_nao_vendidos($id) {


        $this->db->select('lca.USER_ID,lca.LANCE_ID,lca.CONDICIONAL,lca.FECHADO,
   		   		  lca.TOTAL as LANCE_GANHADOR,lca.DATAHORA as HORA_LANCE,us.LOGIN,cm.RAZAO,
   		   		  lt.DESCRICAO as LOTE_DESCRICAO,
   		   		  lt.FECHAMENTO,
   		   		  lt.COMISSAO,
   		   		  COUNT(lt.ID) as TOTAL_LOTES,
   		   		  lt.RETIRADO,
   		   		  lt.NUM_LOTE,lt.VALOR_MINIMO,lt.LANCE_INICIAL,
   		   		  cid.NOME as CIDADE,
   		   		  lc.BIDS,
   		   		  lc.USERS,
   		   		  ufe.UF as UF_LOTE,
   		   		  vst.COUNT as VISITAS,
   		   		  cat.NAME as CATEGORIA,
   		   		  sub.NAME as TIPO_ATIVO,
   		   		  le.*')->from('leiloes as le')
                ->join('(SELECT * FROM lotes WHERE ID NOT IN ( SELECT LOTE_ID FROM lances) GROUP BY ID )  as lt', 'lt.LEILAO_ID=le.ID')
                ->join('visitas_lotes as vst', 'lt.ID=vst.LOTE_ID', 'LEFT')
                ->join('comitentes as cm', 'le.COMITENTE_ID=cm.ID')
                ->join('lances_arrematados as lca', 'lca.LOTE_ID=lt.ID', 'LEFT')
                ->join('users as us', 'us.ID=lca.USER_ID', 'LEFT')
                ->join('cidades as cid', 'lt.CIDADE=cid.ID')
                ->join('tipos_ativos as sub', 'sub.ID=lt.TIPO_ID', 'LEFT')
                ->join('(SELECT LOTE_ID,COUNT(\'ID\') AS BIDS,COUNT(DISTINCT USER_ID) AS USERS FROM lances
                            GROUP BY LOTE_ID) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('categorias as cat', 'cat.ID=sub.CATEGORIA_ID', 'LEFT')
                ->join('estados as ufe', 'cid.ESTADO=ufe.ID')
                ->where('le.ID', $id)
                ->group_by('lt.ID')
                ->order_by('lt.NUM_LOTE', 'ASC');
        $q = $this->db->get();

        return $q->result();
    }

    /*
     * get_lotes_conditionals  Model
     * Model que recupera dados do leiloes condicionais com base no id
     * @param int $id  código do leilão 
     */

    function get_lotes_conditionals($id) {


        $this->db->select('lca.USER_ID,lca.LANCE_ID,lca.CONDICIONAL,
   		   		  lca.TOTAL as LANCE_GANHADOR,lca.DATAHORA as HORA_LANCE,us.LOGIN,cm.RAZAO,
				  lca.VENDIDO,lca.FECHADO,
   		   		  lt.DESCRICAO as LOTE_DESCRICAO,
   		   		  lt.FECHAMENTO,
   		   		  lt.COMISSAO,
   		   		  COUNT(lt.ID) as TOTAL_LOTES,
   		   		  lt.RETIRADO,
   		   		  lt.NUM_LOTE,lt.VALOR_MINIMO,lt.LANCE_INICIAL,
   		   		  cid.NOME as CIDADE,
   		   		  lc.BIDS,
   		   		  lc.USERS,
   		   		  ufe.UF as UF_LOTE,
   		   		  vst.COUNT as VISITAS,
   		   		  cat.NAME as CATEGORIA,
   		   		  sub.NAME as TIPO_ATIVO,
   		   		  le.*')->from('leiloes as le')
                ->join('(SELECT * FROM lotes WHERE ID IN ( SELECT LOTE_ID FROM lances_arrematados WHERE CONDICIONAL="S") GROUP BY ID )  as lt', 'lt.LEILAO_ID=le.ID')
                ->join('visitas_lotes as vst', 'lt.ID=vst.LOTE_ID', 'LEFT')
                ->join('comitentes as cm', 'le.COMITENTE_ID=cm.ID')
                ->join('lances_arrematados as lca', 'lca.LOTE_ID=lt.ID', 'LEFT')
                ->join('users as us', 'us.ID=lca.USER_ID', 'LEFT')
                ->join('cidades as cid', 'lt.CIDADE=cid.ID')
                ->join('tipos_ativos as sub', 'sub.ID=lt.TIPO_ID', 'LEFT')
                ->join('(SELECT LOTE_ID,COUNT(\'ID\') AS BIDS,COUNT(DISTINCT USER_ID) AS USERS FROM lances
                            GROUP BY LOTE_ID) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('categorias as cat', 'cat.ID=sub.CATEGORIA_ID', 'LEFT')
                ->join('estados as ufe', 'cid.ESTADO=ufe.ID')
                ->where('le.ID', $id)
                ->group_by('lt.ID')
                ->order_by('lt.NUM_LOTE', 'ASC');
        $q = $this->db->get();

        return $q->result();
    }

    /**
     *  get_messages_lotes
     *  Recupera mensagens sobre lotes dos leilões que ainda estão abertos
     * */
    function get_messages_lotes() {

        $this->db->select('lt.*,le.ID as COD_LEILAO')
                ->from('leiloes as le')
                ->join('(SELECT * FROM lotes WHERE ID IN ( SELECT LOTE_ID FROM lances_arrematados WHERE CONDICIONAL = "S" AND FECHADO="N") GROUP BY ID) AS lt', 'lt.LEILAO_ID=le.ID')
                ->where(array('le.STATUS' => '4', 'PRESTACAO_CONTAS' => 'N'))
                ->group_by('lt.ID');


        $q = $this->db->get();

        return $q->result();
    }

    function get_closed() {
        
    }

    function get_with_comitentes($id = null) {


        $rs = $this->db->select('cm.*,l.*')
                        ->from('leiloes as l')
                        ->join('comitentes as cm', 'cm.ID=l.COMITENTE_ID', 'left')
                        ->where(array('ID' => $id))
                        ->get()->row();


        return $rs;
    }

    function get_with_comitentes_by_date() {


        $rs = $this->db->select('cm.*,l.*,lt.NUM_LOTES')
                        ->from('leiloes as l')
                        ->join('comitentes as cm', 'cm.ID=l.COMITENTE_ID', 'left')
                        ->join('(SELECT LEILAO_ID, COUNT(ID) AS NUM_LOTES FROM lotes GROUP BY LEILAO_ID ) AS lt', 'lt.LEILAO_ID=l.ID', 'LEFT')
                        ->where(array('DATA >= ' => date('Y-m-d'), 'TIPO <> ' => 'D'))
                        ->or_where('TIPO <> "D" AND `DATA` >= "' . date('Y-m-d', (time() - (20 * 86400))) . '"')
                        ->order_by('l.STATUS,l.DATA', 'ASC')
                        ->get()->result();
        return $rs;
    }

    /**
     * Get Leilao Closed
     * 
     * 
     * @return array de objects
     */
    function get_leilao_closed() {


        $rs = $this->db->select('cm.*,l.*')
                        ->from('leiloes as l')
                        ->join('comitentes as cm', 'cm.ID=l.COMITENTE_ID', 'left')
                        ->or_where('STATUS = 4')
                        ->order_by('l.DATA', 'ASC')
                        ->get()->result();

        return $rs;
    }

    function onClose($id = null) {
        $this->db->trans_start();
        $q = $this->db->select('lt.FECHAMENTO,lt.ID,lt.LEILAO_ID,lt.VALOR_MINIMO,lc.LANCE,lc.USER_ID,(NOT ISNULL(lcp.IS_PLUS)) as HAS_TIME_EXTRA')
                ->from('lotes as lt')
                ->join('leiloes as le', 'le.ID=lt.LEILAO_ID')
                ->join('( SELECT LOTE_ID,LOGIN,ID,USER_ID,TOTAL AS LANCE FROM 
          ( SELECT * FROM lances ORDER BY ID DESC) as lcc
         GROUP BY LOTE_ID ) as lc', 'lc.LOTE_ID=lt.ID', 'LEFT')
                ->join('(SELECT ID,COUNT(DISTINCT ID) as IS_PLUS FROM lotes WHERE ID IN( SELECT LOTE_ID FROM lances_tempo_extra WHERE TEMPO > "' . strtotime('now') . '")) as lcp', 'lcp.ID=lt.ID', 'LEFT')
                ->where('(le.STATUS = 1 or le.STATUS = 2) AND lt.FECHAMENTO < "' . date('Y-m-d H:i:s') . '"')
                //->group_by('lt.ID')
                //->order_by('lc.LANCE','DESC')
                ->get();

        //echo $this->db->last_query();
        //echo '<pre>';
        //  echo strtotime('-30 minutes');
        $this->db->trans_complete();

        if ($this->db->trans_status() == false) {
            return false;
        }

        return $q->result();
        //print_r();
    }

    function data($id) {
        return $this->db->get_where($this->_table_name, array('ID' => $id))->row();
    }

}
